package com.gps.pe.dao;

import java.sql.Connection;
import java.sql.SQLException;

import com.gps.pe.exception.DAOException;

/**
 * The DAO Manager class manage all DAO from central location
 * 
 * @author GPS
 *
 */
public class DAOManagerFactory {

	private Connection connection = null;

	public DAOManagerFactory(final Connection connection) {
		this.connection = connection;
	}

	/**
	 * Returns the Role DAO associated with the current DAOFactory.
	 * 
	 * @return
	 */
	public IRoleDAO getRoleDAO() {
		return new RoleDAOImpl(this.connection);
	}
	
	/**
	 * Return the User DAO associated with the current DAOFactory.
	 * 
	 * @return dao Object of UserDAOImpl class
	 */
	public IUserDAO getUserDAO() {
		return new UserDAOImpl(this.connection);
	}

	/**
	 * Method to begin transaction
	 * 
	 * @throws DAOException
	 */
	public void beginTransaction() throws DAOException {
		try {
			this.connection.setAutoCommit(false);
		} catch (SQLException e) {
			throw new DAOException("Problem to begin transaction", e);
		}
	}
	
	/**
	 * Method to end transaction
	 * 
	 * @throws DAOException
	 */
	public void endTransaction() throws DAOException {
		try {
			this.connection.commit();
			this.connection.close();
		} catch (SQLException e) {
			throw new DAOException("Problem to complete transaction", e);
		}
	}
	
	/**
	 * Method to revert transaction
	 * 
	 * @throws DAOException
	 */
	public void rollbackTransaction() throws DAOException {
		try {
			this.connection.rollback();
			this.connection.close();
		} catch (SQLException e) {
			throw new DAOException("Problem to rollback transaction", e);
		}
	}
}
